from google.appengine.ext import webapp
from google.appengine.api.urlfetch import fetch , GET , POST , DELETE


"""
    proxy apis
"""
class RestApiProxy(webapp.RequestHandler): 

    """
        overide this method to get proxied api server url
    """
    def getApiServer(self):
        return "";

    """
        just forward request , replacing original host
    """
    def proxy(self , method , *args ):
        debug = False 
        if debug:
            self.response.out.write("%s%s?%s" % (self.getApiServer() ,
                                                self.request.path.replace("/twitter/", ""),
                                                self.request.query_string
                                                ))
        else:
            self.response.out.write(fetch("%s%s?%s" % (self.getApiServer() ,
                                                    self.request.path.replace("/twitter/", ""),
                                                    self.request.query_string
                                                    ),
                                            self.request.body,
                                            method,
                                            self.request.headers,
                                            ).content)

    """
        handle http get
    """    
    def get(self, *args):
        self.proxy(GET, *args)
        
    """
        handle http post
    """
    def post(self, *args):
        self.proxy(POST, *args)
    
    """
        handle http delete
    """    
    def delete(self , *arps):
        self.proxy(DELETE, *arps)
